package com.itbaizhan.paymentdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itbaizhan.paymentdemo.controller.dto.OrderInfoDTO;
import com.itbaizhan.paymentdemo.entity.OrderInfo;
import com.itbaizhan.paymentdemo.enums.OrderStatus;
import com.itbaizhan.paymentdemo.mapper.OrderInfoMapper;
import com.itbaizhan.paymentdemo.service.IOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itbaizhan
 * @since 04-26
 */
@Service
@Slf4j
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {

    private OrderInfoMapper orderInfoMapper;
    //实现订单
    @Override
    public OrderInfo createOrder(OrderInfoDTO orderInfoDTO) {
        log.info("*********创建订单********");
        OrderInfo orderInfo = new OrderInfo();

            orderInfo.setTitle(orderInfoDTO.getTitle());
            //long转为string   用时间作为订单编号
            orderInfo.setOrderNo(String.valueOf(System.currentTimeMillis()));
            orderInfo.setUserId(123456L); //用户id
            orderInfo.setTotalFee(orderInfoDTO.getTotalFee());  //金额  分
            //订单状态
            orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
            orderInfo.setCreateTime(LocalDateTime.now());

            //因为继承了orderInfoMapper,而orderInfoMapper 继承了baseMapper
        int insert = baseMapper.insert(orderInfo);
        if(insert > 0){
            log.info("******订单创建成功***********");
        }
        return orderInfo;
    }

    //根据订单编号查询订单信息
    @Override
    public OrderInfo findByOrderNo(String orderNO) {
        LambdaQueryWrapper<OrderInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderInfo::getOrderNo,orderNO);
        List<OrderInfo> orderInfos = baseMapper.selectList(lambdaQueryWrapper);

        if(orderInfos != null && orderInfos.isEmpty()){
            return orderInfos.get(0);
        }

        //如果查不到返回空
        return null;
    }

    /*更新codeUrl*/
    @Override
    public void saveCodeUrl(Long id, String codeUrl) {
        UpdateWrapper<OrderInfo> updateWrapper = new UpdateWrapper<>();
        //设置要更新的字段 key = 数据库列的属性
        updateWrapper.set("code_url",codeUrl);
        //条件
        updateWrapper.eq("id",id);
        //更新
        baseMapper.update(null,updateWrapper);
    }

    /**
     * 根据订单id更新订单的状态
     * @param id   订单id
     * @param orderStatus   订单状态
     */
    @Override
    public void updateOrderStatus(Long id, OrderStatus orderStatus) {

        //LambdaUpdateWrapper 可能不与数据库的列的字段一样的名称
        LambdaUpdateWrapper<OrderInfo> lo = new LambdaUpdateWrapper<>();
        lo.eq(OrderInfo::getId,id);
        lo.set(OrderInfo::getOrderStatus,orderStatus.getType());
        baseMapper.update(null,lo);
    }

    /**
     * 根据订单id更新订单的状态
     * @param orderNO   订单编号
     * @param orderStatus   订单状态
     */
    @Override
    public void updateOrderStatus(String orderNO, OrderStatus orderStatus) {
        //LambdaUpdateWrapper 可能不与数据库的列的字段一样的名称
        LambdaUpdateWrapper<OrderInfo> lo = new LambdaUpdateWrapper<>();
        lo.eq(OrderInfo::getOrderNo,orderNO);
        lo.set(OrderInfo::getOrderStatus,orderStatus.getType());
        baseMapper.update(null,lo);
    }

    /**
     * 超时订单
     * @param minutes  分钟
     * @return
     * 当前时间的5分钟前的时间，
     * 比5分钟前的时间还小，就要查询订单
     */
    @Override
    public List<OrderInfo> getTimeOutOrder(int minutes) {

        //1获取5分钟之前的时间
        Instant instant = Instant.now().minus(Duration.ofMinutes(5));

        //2查询条件构造器
        LambdaQueryWrapper<OrderInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        //3订单类型  未支付
        lambdaQueryWrapper.eq(OrderInfo::getOrderStatus,OrderStatus.NOTPAY.getType());
        //4小于订单创建时间
        lambdaQueryWrapper.le(OrderInfo::getCreateTime,instant);
        //查询
        List<OrderInfo> orderInfos = baseMapper.selectList(lambdaQueryWrapper);
        return orderInfos;

    }
}
